package aiconnect;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
      String WEIGHT_PATH = "C:/Users/NEUSOFT/Desktop/porject_detection/best.pt";
      String IMG_SOURCE_PATH = "C:/Users/NEUSOFT/Desktop/porject_detection/data/images/image_1_3.jpg";
    @Test
      public  void testClient(String[] args) {
        String current_path = System.getProperty("user.dir");
        long start = System.nanoTime();

//        for (int i = 0; i < 10; i++) {
            try {
                Socket s = new Socket("127.0.0.1", 8888);

                //构建IO
                InputStream is = s.getInputStream();
                OutputStream os = s.getOutputStream();

                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
                //向服务器端发送一条消息
                bw.write(WEIGHT_PATH + "#");
                bw.write(IMG_SOURCE_PATH);
                bw.flush();

                //读取服务器返回的消息
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String[] msg = br.readLine().split("#");
                String img_save_path = current_path + "\\" + msg[1];
                String detect_res = msg[0];
                System.out.println("文件路径：" + img_save_path);
                System.out.println("检测结果：" + detect_res);

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
//        }

        long end = System.nanoTime();
        System.out.println("10次检测用时: " + ((end - start) / 1000000) + "ms");
        System.out.println("==============\n");

    }
}